local ClientOHOH = _G.ClientOHOH;
local M = ClientOHOH.class.new(script, ClientOHOH.files.Client_PlayerComponent(),script.Parent.Name);
local trail_cfg = ClientOHOH.config.Trail
local asset_cfg= _G.CommonOHOH.config.Asset
local Players = game:GetService("Players")
local FunctionList = ClientOHOH.load.FunctionList();
local ItemTool=_G.CommonOHOH.load.ItemTool()
local GlobalTipGui = ClientOHOH.load.UIManager().ui_list.GlobalTipGui;
local LanguageHelper=_G.CommonOHOH.LanguageHelper 
function M.new(self,...)
	return M(self,...);
end;


function M.ctor(self, parent)
    M.super.ctor(self, parent);
    self.player=self.parent
    self.DataBase = self.player:GetComponent(ClientOHOH.load.Client_PlayerDataComponent())


    self.TrailModelInsts = {} --模型
    self.TrailEffect = {} --拖尾效果
    self.EquippedTrailInsts = nil --克隆
    self.EquippedTrailId = nil
    self:AddConnect()
end


function M.AddConnect(self)
    self.player.main_inst.CharacterAdded:Connect(function()
        self:AddProcess(function()
            self:Wait(2)
            self:Clear()
            self:GetInstances()
            self:Init()
        end)
    end)

    
end


--获取场景中的对象
function M.GetInstances(self)
    for _,v in pairs(trail_cfg) do
        local trail=ClientOHOH:GetPath_Loop(v.path,workspace)
        table.insert(self.TrailModelInsts,trail)
    end
end

--初始化
function M.Init(self)
    local trailData = self.DataBase:GetData("trail")
    for index, model in pairs(self.TrailModelInsts) do

        --初始化价格，图标，特效
        model.ctrl.price.value.Text = trail_cfg[index].price
        model.ctrl.icon.value.Image = asset_cfg[trail_cfg[index].costId].rid
        self.TrailEffect[index] = model.ctrl.Trail.value
        --按E事件
        local actionText = nil
        if tonumber(trailData.equip) == trail_cfg[index].name then
            --已装备
            if  self:Invoke(FunctionList.equip_trail,trailData.equip,self.TrailEffect[index]) then
                self.EquippedTrailId = index
                model.ctrl.Prompt.value.Enabled = false
                actionText = LanguageHelper:GetText(2)
                self:EquipTrigger(model,index)
            end
        elseif trailData.had[trail_cfg[index].name] ~= nil then
            --已购买，未装备
            actionText = LanguageHelper:GetText(2)
            self:EquipTrigger(model,index)
        else
            --未购买
            actionText = LanguageHelper:GetText(1)
            local connect = nil 
            connect = model.ctrl.Prompt.value.Triggered:Connect(function()
                if self:Buy(trail_cfg[index].name,index) then
                    GlobalTipGui:Show(LanguageHelper:GetText(8))
                    connect:Disconnect()
                    model.ctrl.Prompt.value.Enabled = false
                    model.ctrl.Prompt.value.ActionText = LanguageHelper:GetText(2)
                    self:EquipTrigger(model,index)
                    self:TriggerHandler(model,index)
                else
                    GlobalTipGui:Show(LanguageHelper:GetText(9))
                end
            end)
        end
            model.ctrl.Prompt.value.ActionText = actionText
    end
end


--修改prompt显示
function M.UnloadTrail(self)
    if self.EquippedTrailId then
        local prompt =  self.TrailModelInsts[self.EquippedTrailId].ctrl.Prompt.value
        prompt.Enabled = true
    end
end

function M.EquipTrigger(self,model,index)
    model.ctrl.Prompt.value.Triggered:Connect(function()
        self:TriggerHandler(model,index)
    end)
end

--装备trigger处理
function M.TriggerHandler(self,model,index)
    if  self:Invoke(FunctionList.equip_trail,trail_cfg[index].name,self.TrailEffect[index]) then
        model.ctrl.Prompt.value.Enabled = false
        self:UnloadTrail()
        self.EquippedTrailId = index
    end
end

function M.Clear(self)
    self.TrailModelInsts = {}
    self.TrailEffect = {}
end

--客户端先简单判断，服务器处理
function M.Buy(self,name,index)
    local costCount = trail_cfg[index].price
    local costId = trail_cfg[index].costId
    if not ItemTool.IsRubux(costId) then
        print("不是rubux")
		if not ItemTool.IsHadEnough(costId,costCount,self.DataBase) then
			print("wins不足 client")
            --提示ui
			return false
		end
	end
    return self:Invoke(FunctionList.buy_trail,name,index)
end


function M.dtor(self)
	M.super.dtor(self);
end;

return M